const Net = require('../../common.js')
Page({
  data: {},
  onLoad: function (options) {
    var _this = this

    wx.login({
      success: res => {
        _this.setData({
          code: res.code
        })
      }
    })
  },
  onReady: function () {},
  onShow: function () {},
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {},
  onPageLogin(e) {
    Net.toast('登录中', 'loading')
    var _this = this
    wx.login({
      success: res => {
        Net.log(JSON.stringify(res));
        const userInfo = e.detail;
        var stringInfo = JSON.stringify(userInfo)
        var info = stringInfo.split(''),
          list = ''
        for (let i in info) {
          if (info[i] != ' ') {
            list = list + info[i]
          }
        }
        const data = {
          client_data: list,
          code: res.code
        }
        if (e.detail.userInfo) {
          Net.link('v1/mallLogin', data, function (rep) {
            if (rep.data.code == 201 || rep.data.code == 200) {
              wx.setStorageSync('access_token', rep.data.data.token)
              Net.toast('登录成功', 'success')
              _this.onBackstageCache()
            }
            if (rep.data.code != 201 && rep.data.code != 200) {
              Net.toast(rep.data.msg, 'none')
            }
          }, '', function (err) {})
          wx.setStorageSync('Info', JSON.parse(list))
        } else {
          wx.showModal({
            title: '温馨提示',
            content: '请授权'
          })
        }
      }
    })
  },
  /**
   * 后台缓存数据
   */
  onBackstageCache() {
    Net.link('syncData', {
      is_sync: 1
    }, function (rep) {
      wx.setStorageSync('member', rep.data.data)
      if (rep.data.code == 200) {
        if (rep.data.data.member_info != undefined) {
          wx.setStorageSync('renzheng', 1)
        }
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/dreamBrightness_index/index',
          })
        }, 1500)
      }
    })
  },
  back() {
    wx.navigateBack({
      delta: 1
    })
  }
})